<?php defined('IN_LK') or die('Access Denied');
lk::lo_app('admin_common', 'admin', 0);
class stat extends admin_common
{
	private $db;
	private $position;
	
	public function __construct()
	{
		parent::__construct();
		$this->db = lk::load_model('content');
		$this->position = get_cache('position');
	}
	
	public function initialize()
	{
		$ac   = isset($_GET['type']) ? $_GET['type'] : 'week';
		$start1 = $end1 = $result = '';
		switch($ac)
		{
			case 'week':  //按周统计
				$week = date('w', TIME);
				if($week == 0)
				{
					$week = 7;
				}
				$week --;
				$start = date('Y-m-d', TIME - 86400 * $week);	//比如今天周五 -4天 从周一0点开始
				$start = strtotime($start . ' 00:00:00');
				$result = $this->counts_all($start, 0);
				break;
			case 'month':
				$start = date('Y-m-1', TIME);
				$start = strtotime($start . ' 00:00:00');
				$result = $this->counts_all($start, 0);
				break;
			case 'user':
				$start1 = isset($_GET['start']) ? $_GET['start'] : '';
				$end1   = isset($_GET['end']) ? $_GET['end'] : '';
				$start  = strtotime($start1);
				$end    = strtotime($end1);
				$result = $this->counts_all($start, $end);
				break;
			case 'season':
			default:
				$year = date('Y', TIME);
				$season[1] = strtotime("$year-1-1 00:00:00");
				$season[2] = strtotime("$year-4-1 00:00:00");
				$season[3] = strtotime("$year-7-1 00:00:00");
				$season[4] = strtotime("$year-10-1 00:00:00");
				$year ++;
				$season[5] = strtotime("$year-1-1 00:00:00");
				foreach($season as $kk => $ss)
				{
					if(TIME <$ss)
					{
						$start = $season[$kk - 1];
						break;
					}
				}
				$result = $this->counts_all($start, 0);
				unset($ss);
				break;
		}
		lk::lo('form');
		include self::admin_t('stat');
	}
	
	private function count_by_time($start, $end, $where = '', $order = '', $group = '', $limit = '', $key = '')
	{
		$s = '';
		if($start)
		{
			$s = " AND `inputtime`>'$start'";
		}
		if($end)
		{
			$s .= " AND `inputtime`<'$end'";
		}
		$where = $where ? $where . $s : substr($s, 4);
		if($group || $order)
		{
			return $this->db->get("COUNT(*),`catid`", "$where$s", $order, $group, $limit, $key);
		}
		else
		{
			return $this->db->_count("$where$s");
		}
		
	}
	
	private function counts_all($start, $end)
	{
		$return = array();
		$cats  = get_cache('categorys');
		$return['全部信息'] = $this->count_by_time($start, $end);
		$return['正常信息'] = $this->count_by_time($start, $end, '`status`=\'1\'');
		$return['回收站']  = $this->count_by_time($start, $end, '`status`=\'0\'');
		$return['category']= $this->count_by_time($start, $end, "`status`='1'", "`catid`", "`catid`", '', 'catid');
		foreach($return['category'] as $cat)
		{
			$return[$cats[$cat['catid']]['catname']] =  $cat['COUNT(*)'];
		}
		unset($return['category']);
		return $return;
	}
}